package com.example.demo.controller;

import com.example.demo.anno.Log;
import com.example.demo.pojo.Emp;
import com.example.demo.pojo.PageBean;
import com.example.demo.pojo.Result;
import com.example.demo.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class EmpController {
    @Autowired
    EmpService empService;
    //条件查询员工
    @GetMapping("/emps")
    public Result get(@RequestParam (defaultValue = "1") Integer page,
                      @RequestParam (defaultValue = "5") Integer pageSize,
                      String name, Short gender,
                      @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                      @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        PageBean pageBean = empService.get(page,pageSize,name,gender,begin,end);
        log.info("条件查询员工列表:{},{}",page,pageSize);
        return Result.success(pageBean);
    };
@Log
    //批量删除员工
    @DeleteMapping("/emps/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        empService.delete(ids);
        log.info("批量删除员工:{}",ids);
        return Result.success();
    }

@Log
    //新增员工
    @PostMapping("/emps")
    public Result add(@RequestBody Emp emp){
        empService.add(emp);
        log.info("新增员工:{}",emp);
        return Result.success();
    }

    //根据id查询，页面回显
    @GetMapping("/emps/{id}")
    public Result select (@PathVariable Integer id){
        log.info("根据id查询员工:{}",id);
        Emp emp = empService.select(id);
        return Result.success(emp);
    }
@Log
    @PutMapping("/emps")
    public Result modify(@RequestBody Emp emp){
        log.info("修改员工数据:{}",emp);
        empService.modify(emp);
        log.info("修改员工数据:{}",emp);
        return Result.success();
    }




}
